//{=======================================================================
//! @file A7.c
//! @date 05.10.2013 1:09
//! @author Andrey Drobyshev <immortalguardian1@gmail.com>
//! @ version 1.0
//!
//! @par	Conditions:
//! 		The program reads a natural number and prints its decomposition  into
//!         the sum of the degree's of number 2.
//}=======================================================================

#include <stdio.h>
#include <assert.h>

#define MAX_DEG 31 // The largest degree of number 2 the program works with

//{=================================================================================
//! Function input - initializing the array with the number 2's degrees.
//!
//! @param      a[MAX_DEG]   The array should be initializing.
//!
//! @note       The function has assertion checks.
//}==================================================================================

void input (unsigned int a[]);

//{==============================================================
//! Function main - prints welcome messages and the result,
//! control all the program execution.
//!
//! @return     0 on success
//!
//! @note       The function uses the function "input".
//! @note       The function has assertion checks.
//}===============================================================

int main ()
{
	unsigned int a[MAX_DEG] = {1, 0};   // The array we fill out with number 2's degrees

    short b[MAX_DEG] = {};              // The array controlling if the number 2's degree\
                                        accordiant to the index is included in decomposition

	printf ( "#Hello!\n"
              "#The program prints the decomposition of the"
              " natural number into the sum of the degrees of number 2.\n"
              "#Please enter your number: ");

    input ( a);

    unsigned int n = 0;

	scanf("%u", &n);
	printf("\n");

	int i = 0;

	for ( i = MAX_DEG - 1; i >= 0; i--)
	{
        assert ( 0 <= i && i <= MAX_DEG);

		if ( n > a[i])
		{
			b[i] = 1;
			n = n - a[i];
		}
		else
			if ( n == a[i])
			{
				b[i] = 1;
				i = 0;
			}
    }
	for ( i = 0; i <= MAX_DEG - 1; ++i)
		if ( b[i] == 1)
			printf ( "%d ", a[i]);
	printf ( "\n");
	return 0;
}

void input ( unsigned int a[MAX_DEG])
{
    int i = 0;
    for ( i = 1; i <= MAX_DEG - 1; i++)
	{
        assert ( 0 <= (i - 1) && (i - 1) <= MAX_DEG);
        assert (  0 <= i && i <= MAX_DEG);

		a[i] = a[i - 1] * 2;
    }
}
